yjx.controller('supportDetailController', ['$rootScope', '$scope', '$stateParams', '$timeout', '$location', '$fetch', 'modalService', 'wxShareService', '$locals', 'htmlHandler','$init', function ($rootScope, $scope, $stateParams, $timeout, $location, $fetch, modalService, wxShareService, $locals, htmlHandler,$init) {

    var api = '/support/detail',
        id = $stateParams.id,
        customerId = $stateParams.customerId;
    $scope.forwardId = $stateParams.forwardId;
    $init.title("支持详情")

    $scope.callback = function () {
        /*调用后端记录转发次数的分享*/
        //alert($rootScope.identity )
        if ($rootScope.identity == 1) {
            $fetch.fetch(
                '/recordRelay/content/times',
                {
                    params: {
                        objectId: id,
                        type: 1,
                        customerId:customerId,
                    },
                    type:'POST'
                }
            ).then(function (data) {
                alert(data);
            }).catch(function (e) {
                //alert("报错啦==============");
                modalService.mainMsgError(e.msg)
            })
        }
    }
    function getResource() {
        $scope.surFetching =  true;
        $fetch.fetch(
            api,
            {
                params: {
                    id: id
                },
                cache:false
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.surFetching = false;
                if (data) {
                    data.content = htmlHandler.fontsizeHandler(data.content);
                    $scope.surDetail = data;
                    initWXShare();
                }
               
            })
        }).catch(function (e) {
            $scope.surFetching = false;
            modalService.mainMsgError(e.msg);
        })
    }
    function initWXShare() {
        var wxDesc,wxImg,link;
        wxDesc = $scope.surDetail.name;
        wxImg = $scope.surDetail.images[0];
        // alert($scope.surDetail.name+"wxShare");
        /*android分享链接添加#时自带参数会被微信自动添加的form等参数截掉  原因未知*/
        // if ($rootScope.isAndroid) {
        //     var linkhead = APPSRC
        // } else {
        //     var linkhead = APPSRC + '#/'
        // }
        if ($rootScope.identity == 1) {
            link = APPSRC + "#/surDetail?id=" + id + "&forwardId=" + $rootScope.userId;
        } else {
            // if ($rootScope.isAndroid) {
            //     link = linkhead + "?url=surDetail&id=" + id;
            // } else {
            //     link = linkhead + "surDetail?id=" + id;
            // }
            link = APPSRC + "#/surDetail?id=" + id;
        }
        wxShareService.onHideMenuItems();
        wxShareService.onMenuShareAppMessage('',wxDesc,'',link,$scope.callback)
    }
    function addClick() {
        $fetch.fetch(
            '/update/content/clickTimes',
            {
                params: { contentId: id },
                cache: false
            }
        ).then(function (data) {

        }).catch(function (e) {

        })
    }
    if (id) {
        getResource();
        addClick();
    }

    if ($scope.forwardId && $scope.forwardId != $rootScope.userId) {
        
        $fetch.fetch(
            '/userInfo/getById',
            {
                params: {
                    userId: $scope.forwardId
                }
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.forward = {
                    id: data.user.id,
                    avatar:data.user.avatar,
                    nickname: data.user.realname?data.user.realname:data.user.nickname,
                }
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
       
    }
    if ($scope.forwardId == $rootScope.userId) {
        $scope.forward = {
            nickname: $rootScope.user.nickname,
            avatar: $rootScope.user.avatar
        }
    }

    if (!$scope.forwardId && $rootScope.identity != 1) {
        angular.element(document.getElementById('surContent')).addClass('surContent2')
    }
    // $timeout(function(){
    //     $rootScope.changeState=true;
    //     $rootScope.changeStateName="support";
    // })
}])